package codetop.microsoft.T23;

import util.ListNode;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @Author: 18362
 * @Create: 2022-08-15 0:53:20 星期一
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        Queue<ListNode> queue = new PriorityQueue<>((a, b) -> (a.val - b.val));
        for (int i=0; i<lists.length; ++i) {
            if (lists[i] != null)
                queue.offer(lists[i]);
        }
        ListNode dummy = new ListNode(), p = dummy;
        while (!queue.isEmpty()) {
            ListNode head = queue.poll();
            p.next = head;
            p = p.next;
            if (head.next != null)
                queue.offer(head.next);
        }
        return dummy.next;
    }
}